package com.farsight.leo.fssmartlock.activities;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;

import com.covics.zxingscanner.OnDecodeCompletionListener;
import com.covics.zxingscanner.ScannerView;
import com.farsight.leo.fssmartlock.Constants;
import com.farsight.leo.fssmartlock.R;
import com.farsight.leo.fssmartlock.bases.BaseActivity;
import com.farsight.leo.fssmartlock.utils.ToastUtils;

import java.util.Random;

public class BarCodeScanningActivity extends BaseActivity {

    private ScannerView mScannerView;
    private String mScanResult;

    @Override
    protected int set_layout_id() {
        return R.layout.activity_bar_code_scanning;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        mScannerView = findViewById(R.id.activity_bar_code_scanning_scanner_view);
    }

    @Override
    protected void initListeners() {
        mScannerView.setOnDecodeListener(new OnDecodeCompletionListener() {
            @Override
            public void onDecodeCompletion(String s, String s1, Bitmap bitmap) {
                mScanResult = s1;
                ToastUtils.showToast("扫描成功！");
                setResult();
                finish();
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        mScannerView.onPause();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mScannerView.onResume();
    }


    private void setResult() {
        Intent intent = new Intent();
        intent.putExtra(Constants.KEY_BAR_CODE, mScanResult == null ? "" : mScanResult);
        setResult(Constants.RESULT_CODE_BAR_CODE, intent);
    }

    public void genSimulationCode(View view) {
        mScanResult = String.valueOf(new Random().nextInt(999)+10000)+"@abc";
        ToastUtils.showToast("扫描成功！");
        setResult();
        finish();
    }
}
